<template>
	<div>
		<el-card shadow="hover">
			<div class=" mb15">
				<el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="120px">
					<el-row>
						<el-col :span="6">
							<el-form-item label="创建时间">
								<el-date-picker v-model="tableData.dateRange" type="datetimerange" size="default"
									style="width: 300px" range-separator="-" start-placeholder="开始日期"
									end-placeholder="结束日期"></el-date-picker>
							</el-form-item>
						</el-col>

						<el-col :span="6">
							<el-form-item label="平台" prop="client">
								<el-select v-model="tableData.param.client" placeholder="请选择平台" clearable>
									<el-option label="IOS" :value="1" />
									<el-option :value="2" label="Android" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item label="状态" prop="status">
								<el-select v-model="tableData.param.status" placeholder="请选择状态" clearable>
									<el-option :value="1" label="开启" />
									<el-option :value="2" label="关闭" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="6">
							<el-form-item>
								<el-button type="primary" @click="getList"><el-icon>
										<Search />
									</el-icon>搜索</el-button>
								<el-button @click="resetQuery(queryRef)"><el-icon>
										<Refresh />
									</el-icon>重置</el-button>
								<el-button type="primary" @click="handleAdd" v-if="permissionStore.isPermissionsBtn('012001001')"><el-icon>
										<Plus />
									</el-icon>新增</el-button>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</div>
			<el-table v-loading="loading" :data="tableData.data">
				<el-table-column label="图片" align="center" min-width="100px">
					<template #default="{ row }">
						<el-image :src="row.img" style="width: 200px;height: 75px;" preview-teleported
							:preview-src-list="[row.img]" />
					</template>
				</el-table-column>
				<el-table-column label="平台" align="center" prop="client" min-width="100px">
					<template #default="{ row }">{{ row.client == 1 ? 'IOS' : row.client == 2 ? 'Android' : 'IOS+Android'
						}}</template>
				</el-table-column>
				<el-table-column label="打开方式" align="center" prop="way" min-width="100px">
					<template #default="{ row }">{{ row.client == 0 ? '不做处理' : row.client == 1 ? '内部浏览器' : '外部浏览器' }}</template>
				</el-table-column>
				<el-table-column label="状态" align="center"  min-width="100px">
					<template #default="{ row }">
						<el-switch v-model="row.status" :active-value="1" :inactive-value="2" inline-prompt
							active-text="开启" inactive-text="关闭" @change="handleStatusChange(row)" :disabled="!permissionStore.isPermissionsBtn('012001002')"></el-switch>
					</template>
				</el-table-column>
				<el-table-column label="创建时间" align="center" prop="createTime" :formatter="formatDateTime"
					min-width="150px" />

				<el-table-column label="操作" align="center" v-if="permissionStore.isPermissionsBtn(['012001003','012001004'])"
					class-name="small-padding" min-width="150px" fixed="right">
					<template #default="scope">
						<el-button  v-if="permissionStore.isPermissionsBtn('012001003')" text type="primary"
							@click="handleUpdate(scope.row)"><el-icon>
								<EditPen />
							</el-icon>修改</el-button>
						<el-button  v-if="permissionStore.isPermissionsBtn('012001004')" text type="danger"
							@click="handleDel(scope.row)"><el-icon>
								<Delete />
							</el-icon>删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNumber"
				v-model:limit="tableData.param.pageLength" @pagination="getList" />
		</el-card>
		<addView ref="addRef" @getList="getList"></addView>
		<editView ref="editRef" @getList="getList"></editView>
	</div>
</template>
<script lang="ts">
import { editBannerStatusApi, bannerListApi, delBannerApi } from '@/api/operationsManage/banner';
import { toRefs, reactive, onMounted, ref, defineComponent, computed, getCurrentInstance, toRaw } from 'vue';
import { ElMessage, ElMessageBox, FormInstance } from 'element-plus';
import { usePermissionStore } from "@/store/modules/permission"
import editView from './component/edit.vue';
import addView from './component/add.vue';
import { updateParamsWithDateRange, formatDateTime } from '@/utils/index'
import { Delete } from '@element-plus/icons-vue';
export default defineComponent({
	name: 'bannerList', //banner列表
	components: {
		editView,
		addView
	},
	setup() {
		const { proxy } = <any>getCurrentInstance();
		const loading = ref(false);
		const queryRef = ref();
		const addRef = ref()
		const editRef = ref();
		const permissionStore = usePermissionStore()
		// 是否显示所有搜索选项
		const showAll = ref(false);

		const word = computed(() => {
			if (showAll.value === false) {
				//对文字进行处理
				return '展开搜索';
			} else {
				return '收起搜索';
			}
		});
		const state = reactive({
			tableData: {
				data: [],
				total: 0,
				loading: false,
				dateRange: [],

				param: {
					pageNumber: undefined,
					pageLength: undefined,
					status: undefined,
					client: undefined,
					beginTime: undefined,
					endTime: undefined,
				},
			},
		});

		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		// 初始化表格数据
		const initTableData = () => {
			getList();
		};
		/** 重置按钮登录 */
		const resetQuery = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			formEl.resetFields();
			state.tableData.dateRange = []
			getList();
		};
		// 获取列表数据
		const getList = () => {
			loading.value = true;
			const obj = updateParamsWithDateRange(state.tableData.dateRange)
			Object.assign(state.tableData.param, obj);
			bannerListApi(state.tableData.param).then((res: any) => {
				let list = res.data.list ?? [];
				state.tableData.data = list
				state.tableData.total = res.data.total;
				loading.value = false;
			});
		};
		const toggleSearch = () => {
			showAll.value = !showAll.value;
		};
		const handleAdd = () => {
			addRef.value.openDialog()
		}
		const handleUpdate = (row) => {
			const rawRow = structuredClone(toRaw(row));
			editRef.value.openDialog(rawRow);
		};
		const handleDel = (row) => {

			let msg = `此操作将永久删除，是否继续?`
			ElMessageBox.confirm(msg, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(() => {
					delBannerApi({id:row.id}).then(() => {
						ElMessage.success('删除成功');
						getList();
					});
				})
				.catch(() => {});
			
		}
		// 状态修改
		const handleStatusChange = (row: any) => {
			let text = row.status == 1 ? '开启' : '关闭';
			ElMessageBox.confirm('确认要[' + text  + ']吗?', "警告", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				type: "warning"
			}).then(function () {
				const { id,status } = row
				let obj = {
					id,status,	//状态
				}
				return editBannerStatusApi(obj);
			}).then(() => {
				ElMessage.success(text + "成功");
			}).catch(function () {
				row.status = row.status == 1 ? 2 : 1;
			});
		};
		return {
			proxy,
			editRef,
			showAll,
			loading,
			word,
			queryRef,
			addRef,
			permissionStore,
			resetQuery,
			getList,
			toggleSearch,
			handleUpdate,
			handleAdd,
			formatDateTime,
			handleDel,
			handleStatusChange,
			...toRefs(state),
		};
	},
});
</script>
<style lang="scss" scoped>
.colBlock {
	display: block;
}

.colNone {
	display: none;
}
</style>
